package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/longest-happy-string/
 * @create 2025/10/29 20:51
 * @since JDK17
 */

public class Solution82 {
    public String longestDiverseString(int a, int b, int c) {
        StringBuffer ans = new StringBuffer();
        while (a > 0 || b > 0 || c > 0) {
            boolean wa = false, wb = false, wc = false;
            int n = ans.length();
            if (n >= 2 && ans.charAt(n - 1) == ans.charAt(n - 2)) { // 前两位一样
                char x = ans.charAt(n - 1);
                if (x == 'a') {
                    if (b > c) {
                        wb = true;
                    } else {
                        wc = c > 0;
                    }
                } else if (x == 'b') {
                    if (a > c) {
                        wa = true;
                    } else {
                        wc = c > 0;
                    }
                } else {
                    if (a > b) {
                        wa = true;
                    } else {
                        wb = b > 0;
                    }
                }
            } else {
                if (a >= b && a >= c) {
                    wa = true;
                } else if (b >= a && b >= c) {
                    wb = true;
                } else {
                    wc = true;
                }
            }
            if (wa) {
                a--;
                ans.append('a');
            }
            if (wb) {
                b--;
                ans.append('b');
            }
            if (wc) {
                c--;
                ans.append('c');
            }
            if (!wa && !wb && !wc) {
                break;
            }
        }

        return ans.toString();
    }
}
